if SERVER then
	AddCSLuaFile("shared.lua")
	SWEP.Weight				= 5
	SWEP.AutoSwitchTo		= false
	SWEP.AutoSwitchFrom		= false
end

if CLIENT then

	surface.CreateFont("csd", ScreenScale(30), 500, true, true, "CSKillIcons")
	surface.CreateFont("csd", ScreenScale(60), 500, true, true, "CSSelectIcons")

	SWEP.DrawAmmo			= true
	SWEP.DrawCrosshair		= false
	SWEP.ViewModelFOV		= 65
	SWEP.ViewModelFlip		= true
	SWEP.CSMuzzleFlashes	= true
	SWEP.PrintName = "ZOMBY SHOOTOR 900"
	SWEP.IconLetter = "a"
	SWEP.IconFont = "CSSelectIcons"
	
	function SWEP:DrawWeaponSelection( x, y, wide, tall, alpha )
		draw.SimpleText( self.IconLetter, self.IconFont, x + wide/2, y + tall*0.2, Color( 15, 20, 200, 255 ), TEXT_ALIGN_CENTER )
	end
	
end

SWEP.Author			= "Rambo_6"
SWEP.Contact		= ""
SWEP.Purpose		= ""
SWEP.Instructions	= ""

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= false

SWEP.ViewModel	= ""
SWEP.WorldModel = "models/weapons/w_package.mdl"

SWEP.Primary.Sound			= Sound("Weapon_USP.Single")
SWEP.Primary.Recoil			= 1
SWEP.Primary.Damage			= 1
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.02
SWEP.Primary.Delay			= 0.15

SWEP.Primary.ClipSize		= 1
SWEP.Primary.DefaultClip	= 13337
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

function SWEP:GetWorldModel()
	return self.WorldModel or "models/weapons/w_package.mdl"
end

function SWEP:GetMaxAmmo()
	return self.Primary.ClipSize or 1
end

function SWEP:GetUsedAmmoType()
	return self.Primary.Ammo or "none"
end

function SWEP:Initialize()
	if SERVER then
		self:SetWeaponHoldType(self.HoldType)
	end
end

function SWEP:Deploy()
	self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	self:SetIronsights(false)
	return true
end  

function SWEP:Think()	
	if self.Weapon:Clip1() < 1 then
		self:SetIronsights(false)
	end
end

function SWEP:CanPrimaryAttack()
	if self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) <= 0 then
		self.Weapon:EmitSound( "Weapon_Pistol.Empty", 100, math.random(90,140) )
		self.Weapon:SetNextPrimaryFire(CurTime() + .4)
		return false
	end
	if self.Weapon:Clip1() <= 0 then
		self.Weapon:SetClip1(0)
		self.Weapon:SetNextPrimaryFire(CurTime() + .4)
		self.Weapon:EmitSound( "Weapon_Pistol.Empty", 100, math.random(90,120) )
		self.Weapon:SetNetworkedBool( "Ironsights", false )
		self.Weapon:DefaultReload(ACT_VM_RELOAD)
		if math.random(1,2) == 1 and SERVER then
			self.Owner:VoiceSound(ChooseRandom(self.Owner.Reload),100,math.Rand(0,0.2))
		end
		return false
	end
	return true
end

function SWEP:Ammo1()
	if CLIENT then return self.Owner:GetNWInt("ammo_"..self.Weapon:GetPrimaryAmmoType()) end
    return self.Owner:GetAmmunition( self.Weapon:GetPrimaryAmmoType() )
end 

function SWEP:TakePrimaryAmmo( num )
	if ( self.Weapon:Clip1() <= 0 ) then 
		if ( self:Ammo1() <= 0 ) then return end
	return end
	if SERVER then
		self.Owner:TakeAmmunition( self.Primary.Ammo, num )
	end
	self.Weapon:SetClip1( self.Weapon:Clip1() - num )	
end

function SWEP:PrimaryAttack()
	if not self:CanPrimaryAttack() then return end
	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)

	self.Weapon:EmitSound(self.Primary.Sound,100,math.random(98,102))
	self:ShootBullets(self.Primary.Damage, self.Primary.NumShots, self.Primary.Cone)
	self:TakePrimaryAmmo(1)
	self:SetRecoilTime(CurTime() + .2)
	if SERVER then
		self.Owner:ViewPunch(Angle(math.Rand(-0.2,-0.1) * self.Primary.Recoil, math.Rand(-0.1,0.1) *self.Primary.Recoil, 0))
	end
end

function SWEP:ShootBullets( damage, numbullets, aimcone)

	local scale = 1
	local canscale = true
	if self.Owner:KeyDown(IN_DUCK) then
		scale = math.Clamp(aimcone/1.5,0,10)
		canscale = false
	elseif self.Owner:KeyDown(IN_WALK) and canscale then
		if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
			scale = aimcone
			canscale = false
		else
			scale = aimcone
			canscale = false
		end
	elseif (self.Owner:KeyDown(IN_SPEED) or self.Owner:KeyDown(IN_JUMP)) and canscale then
		scale = aimcone + (aimcone*2)
		canscale = false
	elseif (self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT)) and canscale then
		scale = aimcone + (aimcone*1.5)
	else
		scale = aimcone
	end
	
	local recoil = self.Weapon:GetRecoilRatio()
	scale = scale * recoil
	
	local irons = self.Weapon:GetNetworkedBool( "Ironsights", false )
	local tracer = 1
	if irons then
		tracer = 90
	end
	
	local bullet = {}
	bullet.Num 		= numbullets
	bullet.Src 		= self.Owner:GetShootPos()			
	bullet.Dir 		= self.Owner:GetAimVector()			
	bullet.Spread 	= Vector( scale, scale, 0 )		
	bullet.Tracer	= tracer		
	bullet.Force	= math.Round(damage * 1.1)							
	bullet.Damage	= math.Round(damage * GameInfo.HumanDamageScale)
	bullet.AmmoType = "Pistol"
	bullet.TracerName 	= "Tracer"
	bullet.Callback = function ( attacker, tr, dmginfo )
		if tr.HitWorld or not tr.Entity then return end
		if not tr.Entity:IsPlayer() and not string.find(tr.Entity:GetClass(),"npc") then return end
		if tr.Entity:IsPlayer() then
			if tr.Entity:Team() == TEAM_HUMANS then return end
		end
		if tr.HitGroup == 1 and tr.Entity:IsValid() then
			local t2 = {}
			t2.start = tr.HitPos
			t2.endpos = self.Owner:GetAimVector() * 500
			t2.filter = tr.Entity
			local tr2 = util.TraceLine(t2)
			util.Decal("Blood", tr2.HitPos + tr2.HitNormal, tr2.HitPos - tr2.HitNormal)
			local effectdata = EffectData()
			effectdata:SetEntity(tr.Entity)
			effectdata:SetNormal(tr.HitNormal)
			effectdata:SetOrigin(tr.HitPos)
			util.Effect("headshot", effectdata)
			if math.random(1,5) == 1 then
				local effectdata = EffectData()
				effectdata:SetAngle(self.Owner:GetAimVector():Angle())
				effectdata:SetOrigin(tr.HitPos)
				util.Effect("head_gib", effectdata)
			end
			tr.Entity:EmitSound(ChooseRandom(GameSounds.HeadShot), 100, math.random(90,120))
			if tr.Entity:GetPos():Distance(self.Owner:GetPos()) < 100 and SERVER then
				umsg.Start("BloodStain", self.Owner)
				umsg.End() 
			end
		else
			util.Decal("Blood", tr.HitPos + tr.HitNormal, tr.HitPos - tr.HitNormal)
			local effectdata = EffectData()
			effectdata:SetOrigin(tr.HitPos)
			effectdata:SetNormal(tr.HitNormal)
			util.Effect("bodyshot", effectdata)
			local t2 = {}
			t2.start = tr.HitPos
			t2.endpos = self.Owner:GetAimVector() * 500
			t2.filter = tr.Entity
			local tr2 = util.TraceLine(t2)
			util.Decal("Blood", tr2.HitPos + tr2.HitNormal, tr2.HitPos - tr2.HitNormal)
			if tr.Entity:GetPos():Distance(self.Owner:GetPos()) < 100 and SERVER then
				umsg.Start("BloodStain", self.Owner)
				umsg.End() 
			end
		end
	end
	
	self.Owner:FireBullets( bullet )
	
	self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK ) 		// View model animation
	self.Owner:MuzzleFlash()							// Crappy muzzle light
	self.Owner:SetAnimation( PLAYER_ATTACK1 )			// 3rd Person Animation
	
end

function SWEP:SetRecoilTime( t )
	self.Weapon:SetNetworkedFloat( "Recoil", t )
end

function SWEP:GetRecoilTime()
return self.Weapon:GetNetworkedFloat( "Recoil" ) or CurTime() end

function SWEP:GetRecoilRatio()
	local rtime = self.Weapon:GetRecoilTime()
	local ratio = 2 - math.Clamp( (CurTime() - rtime) * 4, 0.0, 1.0 )
	return ratio
end

SWEP.CrosshairScale = 1
function SWEP:DrawHUD()

	local ironsight = self.Weapon:GetNetworkedBool( "Ironsights" )
	
	if ironsight then return end

	local x = ScrW() * 0.5
	local y = ScrH() * 0.5
	local scalebywidth = (ScrW() / 1024) * 10

	local scale = 1
	local canscale = true
	if self.Owner:KeyDown(IN_DUCK) then
		scale = scalebywidth * math.Clamp(self.Primary.Cone/1.5,0,10)
		canscale = false
	elseif self.Owner:KeyDown(IN_WALK) and canscale then
		if self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT) then
			scale = scalebywidth * self.Primary.Cone
			canscale = false
		else
			scale = scalebywidth * self.Primary.Cone
			canscale = false
		end
	elseif (( self.Owner:KeyDown(IN_SPEED) and self.Owner:GetVelocity() != Vector(0,0,0) ) or self.Owner:KeyDown(IN_JUMP)) and canscale then
		scale = scalebywidth * self.Primary.Cone + (self.Primary.Cone*12)
		canscale = false
	elseif (self.Owner:KeyDown(IN_FORWARD) or self.Owner:KeyDown(IN_BACK) or self.Owner:KeyDown(IN_MOVELEFT) or self.Owner:KeyDown(IN_MOVERIGHT)) and canscale then
		scale = scalebywidth * self.Primary.Cone + (self.Primary.Cone*6)
	else
		scale = scalebywidth * self.Primary.Cone
	end
	
	local recoil = self.Weapon:GetRecoilRatio()
	scale = scale * recoil

	if self.Owner:KeyDown(IN_ZOOM) then
		scale = scale * 5
	end

	surface.SetDrawColor(220, 220, 220, 220)

	local dist = math.abs(self.CrosshairScale - scale)
	self.CrosshairScale = math.Approach(self.CrosshairScale, scale, FrameTime() * 2 + dist * 0.05)

	local gap = 40 * self.CrosshairScale
	local length = gap + 15 * self.CrosshairScale
	surface.DrawLine(x - length, y, x - gap, y)
	surface.DrawLine(x + length, y, x + gap, y)
	surface.DrawLine(x, y - length, x, y - gap)
	surface.DrawLine(x, y + length, x, y + gap)

	//surface.DrawLine(x-2, y, x+2, y)
	//surface.DrawLine(x, y-2, x, y+2)
end

local IRONSIGHT_TIME = 0.3
function SWEP:GetViewModelPosition( pos, ang )

	if ( !self.IronSightsPos ) then return pos, ang end

	local bIron = self.Weapon:GetNetworkedBool( "Ironsights" )
	
	if ( bIron != self.bLastIron ) then
		self.bLastIron = bIron 
		self.fIronTime = CurTime()
		if ( bIron ) then 
			self.SwayScale 	= 0.3
			self.BobScale 	= 0.1
		else 
			self.SwayScale 	= 1.0
			self.BobScale 	= 1.0
		end
	end
	
	local fIronTime = self.fIronTime or 0

	if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then 
		return pos, ang 
	end
	
	local Mul = 1.0
	
	if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then
		Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 )
		if (!bIron) then Mul = 1 - Mul end
	end

	local Offset	= self.IronSightsPos
	
	if ( self.IronSightsAng ) then
		ang = ang * 1
		ang:RotateAroundAxis( ang:Right(), 		self.IronSightsAng.x * Mul )
		ang:RotateAroundAxis( ang:Up(), 		self.IronSightsAng.y * Mul )
		ang:RotateAroundAxis( ang:Forward(), 	self.IronSightsAng.z * Mul )
	end
	
	local Right 	= ang:Right()
	local Up 		= ang:Up()
	local Forward 	= ang:Forward()

	pos = pos + Offset.x * Right * Mul
	pos = pos + Offset.y * Forward * Mul
	pos = pos + Offset.z * Up * Mul

	return pos, ang
end

SWEP.NextSecondaryAttack = 0
function SWEP:SecondaryAttack()
	if not self.IronSightsPos then return end
	if self.NextSecondaryAttack > CurTime() then return end
	if self.Weapon:Clip1() < 1 then return end
	
	bIronsights = not self.Weapon:GetNetworkedBool( "Ironsights", false )
	
	self:SetIronsights( bIronsights )
	self.NextSecondaryAttack = CurTime() + 0.3
end

function SWEP:SetIronsights( b )
	self.Weapon:SetNetworkedBool( "Ironsights", b )
end

function SWEP:Reload()

	if self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) <= self.Weapon:Clip1() then return end
	
	if self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) <= 0 then return end

	self.Weapon:SetNetworkedBool( "Ironsights", false )
	if self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) <= self.Primary.ClipSize then
		self.Weapon:DefaultReload(ACT_VM_RELOAD)
		self.Weapon:SetClip1( self.Owner:GetNWInt("ammo_"..self.Primary.Ammo, 0) )
	else
		self.Weapon:DefaultReload(ACT_VM_RELOAD)
	end
end
